package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketAd;
import com.cskaoyan.market.db.domain.MarketAdExample;
import com.cskaoyan.market.db.mapper.MarketAdMapper;
import com.cskaoyan.market.service.AdminAdUpdateService;
import com.cskaoyan.market.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author Meltedicecream
 * @since 2023/06/20 21:09
 */
public class AdminAdUpdateServiceImpl implements AdminAdUpdateService {
    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    @Override
    public MarketAd update(Integer id, String name, String url, String content, String link, Byte position, Boolean enabled, String addTime, String updateTime) {
        SqlSession session = MybatisUtil.getSession();
        MarketAdMapper marketAdMapper = session.getMapper(MarketAdMapper.class);

        //为market对象赋值
        MarketAd marketAd = new MarketAd();
        marketAd.setId(id);
        marketAd.setName(name);
        marketAd.setUrl(url);
        marketAd.setContent(content);
        marketAd.setLink(link);
        marketAd.setPosition(position);
        marketAd.setEnabled(enabled);
        marketAd.setAddTime(LocalDateTime.parse(addTime,DATE_TIME_FORMATTER));
        LocalDateTime now = LocalDateTime.now();
        marketAd.setUpdateTime(now);

        //通过id查询修改数据库数据
        marketAdMapper.updateByPrimaryKey(marketAd);
        session.commit();
        session.close();
        return marketAd;
    }
}
